/*
 * 工作花样选择
 * 舞者无罪
 * 2014.12.01
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <minigui/common.h>
#include <minigui/minigui.h>
#include <minigui/gdi.h>
#include <minigui/window.h>
#include <minigui/control.h>
#include "resource.h"
#include "func.h"
#include "lang.h"
#include "mhbtn.h"
#include "mhedit.h"
#include "mhlabel.h"
#include "file_select.h"
#include "com.h"
#include "info.h"
#include "run.h"
#include "utf8_gb2312.h"
#include "progress_all.h"

/*
 * show file list
 */
static void ShowFileList(HWND hWnd)
{
    WND_FILE *pwnd = (WND_FILE*)GetWindowAdditionalData(hWnd);
    if(pwnd)
    {
        int size = __min(pwnd->list_size, FILE_LIST_NUM_BY_FILE_BMP);
        int i = 0;
        FILE_INFO *p = pwnd->file_list;
        char name[MH_FILE_NAME_MAX_LEN] = {0};
        for(i = 0; i < size; ++i)
        {
            HWND hWndEdit = GetDlgItem(hWnd, IDC_EDIT1 + i);
            const char eg[] = "0001 123456789ABC 1001024 14-12-18 20:27:30 [1600 x 99999]";
            char sc[sizeof(eg)] = {0};
            int index = pwnd->over_top + i;
            sprintf(sc, "%4u ", index + 1);
            UTF_8ToGB2312(name, p[index].file_name, strlen(p[index].file_name) + 1);
            Truncate(&sc[5], name, 13);
            sprintf(&sc[18], "%7u ", p[index].size);
            if(strlen(sc) > 26)
            {
                sprintf(&sc[18], "%6uK ", p[index].size / 1024 + 1);
            }
            struct tm *tm_now;
            tm_now = localtime(&p[index].modify_time);
            sprintf(&sc[26], "%02u-%02u-%02u %02u:%02u:%02u [%u x %u]",
                tm_now->tm_year % 100, tm_now->tm_mon + 1, tm_now->tm_mday,
                tm_now->tm_hour, tm_now->tm_min, tm_now->tm_sec,
                p[index].width, p[index].height);
            EnableWindow(hWndEdit, TRUE);
            SetWindowCaption(hWndEdit, sc);
            InvalidateRect(hWndEdit, NULL, TRUE);
        }
        for(; i < FILE_LIST_NUM_BY_FILE_BMP; ++i)
        {
            HWND hWndEdit = GetDlgItem(hWnd, IDC_EDIT1 + i);
            EnableWindow(hWndEdit, FALSE);
            SetWindowCaption(hWndEdit, "");
            InvalidateRect(hWndEdit, NULL, TRUE);
        }
    }
}
/*
 *
 */
static void CreateFileSelectInterface(HWND hWnd)
{
    //sys btn
    CreateWindow (MH_STATIC_NAME,
        "",
        WS_CHILD | WS_VISIBLE,
        ID_FALSE,
        CX_FILE_BMP - CX_ASK_BMP * 2,
        10,
        CX_ASK_BMP,
        CY_ASK_BMP,
        hWnd, (int)&g_bmp[BMP_FALSE]);
    CreateWindow (MH_STATIC_NAME,
        "",
        WS_CHILD | WS_VISIBLE,
        ID_TRUE,
        CX_FILE_BMP - CX_ASK_BMP * 3 - 8,
        10,
        CX_ASK_BMP,
        CY_ASK_BMP,
        hWnd, (int)&g_bmp[BMP_TRUE]);
    CreateWindow (MH_STATIC_NAME,
        "",
        WS_CHILD | WS_VISIBLE,
        ID_ESC,
        CX_FILE_BMP - CX_ASK_BMP * 4 - 8 * 2,
        10,
        CX_ASK_BMP,
        CY_ASK_BMP,
        hWnd, (int)&g_bmp[BMP_ASK]);
    int top = 52;
    int left = 11;
    int i = 0;
    for(i = 0; i < FILE_LIST_NUM_BY_FILE_BMP; ++i)
    {
        CreateWindow(MH_EDIT_NAME,
            "",
            WS_CHILD | WS_VISIBLE,
            IDC_EDIT1 + i,
            left,
            top + i * 20,
            460,
            20,
            hWnd, MH_EDIT_TEXT_WHITE | MH_EDIT_ACTIVE_DISABLE);
    }
    int size = GetFileNum(KNIT_FILE_DIR);
    WND_FILE *pwnd = (WND_FILE*)calloc(1, sizeof(WND_FILE));
    SetWindowAdditionalData(hWnd, (int)pwnd);
    pwnd->sort_file_type = GetRunFileSortType();
    if(size)
    {
        FILE_INFO *p = calloc(size, sizeof(FILE_INFO));
        char *file_list = calloc(size, sizeof(p->file_name));
        char *suffix_list = calloc(size, sizeof(p->file_suffix));
        GetFileName(KNIT_FILE_DIR, (char (*)[sizeof(p->file_name)])file_list,
                (char(*)[sizeof(p->file_suffix)])suffix_list, size);
        GetFileInfoList(p, (char (*)[sizeof(p->file_name)])file_list,
                (char(*)[sizeof(p->file_suffix)])suffix_list, size, KNIT_FILE_DIR);
        free(file_list);
        free(suffix_list);
        pwnd->file_list = p;
        pwnd->list_size = size;
        if(pwnd->sort_file_type)
        {
            SortFileList(pwnd->file_list, pwnd->list_size, pwnd->sort_file_type);
        }
        ShowFileList(hWnd);
    }
}
/*
 * select knit file
 */
static int SelectKnitFile(HWND hWnd)
{
    int ret = 0;
    WND_FILE *p = (WND_FILE*)GetWindowAdditionalData(hWnd);
    if(p)
    {
        int index = p->over_top + p->active;
        if(index < p->list_size)
        {
            SafeFreeAuxConList(g_knit_data);
            SetKnitFileName(g_knit_data, p->file_list[index].file_name);

            char sc[MH_FILE_NAME_MAX_LEN + sizeof(KNIT_FILE_DIR) + sizeof(".con")] = {0};
            strcat(sc, KNIT_FILE_DIR);
            strcat(sc, p->file_list[index].file_name);
            int len = strlen(sc);
            printf("\n");
            strcat(sc, ".dat");
            printf("read data from :%s\n",sc);
            ret = ReadDatFile(sc, g_knit_data);
            if(ret)
            {
                printf("read flower data: %d\n", ret);
                goto FAIL;
            }
            else
            {
                SetFlowerBoundary(g_knit_data);
                SendFlowerData(g_knit_data);
            }

            SetProgressData(g_knit_data, 20, (char *)GetString(923));
            SendMessage(GetProgressHwnd(g_knit_data), MSG_UPDATE, 0, 0);

            sc[len] = 0;
            strcat(sc, ".con");
            printf("read data from :%s\n",sc);
            ret = ReadConFile(sc, g_knit_data);
            if(ret)
            {
                printf("read control data: %d\n", ret);
                goto FAIL;
            }
            else
            {
                if(GetRockCompressEnable(g_knit_data))
                {
                    CompressRockCtrlPage(g_knit_data);
                }
                SendControlData(g_knit_data);
            }

            SetProgressData(g_knit_data, 40, (char *)GetString(923));
            SendMessage(GetProgressHwnd(g_knit_data), MSG_UPDATE, 0, 0);
            sc[len] = 0;
            strcat(sc, ".spd");
            printf("read data from :%s\n",sc);
            ret = ReadSpeedFile(sc, g_knit_data);
            if(ret)
            {
                printf("read speed file: %d\n", ret);
            }
            else
            {
                SendSpeedParam(g_knit_data);
            }

            SetProgressData(g_knit_data, 60, (char *)GetString(923));
            SendMessage(GetProgressHwnd(g_knit_data), MSG_UPDATE, 0, 0);
            sc[len] = 0;
            strcat(sc, ".stp");
            printf("read data from :%s\n",sc);
            ret = ReadDumuFile(sc, g_knit_data);
            if(ret)
            {
                printf("read dumu file: %d\n", ret);
            }
            else
            {
                SendDumuParam(g_knit_data);
            }
            SetProgressData(g_knit_data, 80,  (char *)GetString(923));
            SendMessage(GetProgressHwnd(g_knit_data), MSG_UPDATE, 0, 0);
            sc[len] = 0;
            strcat(sc, ".gsp");
            printf("read data from :%s\n",sc);
            ret = ReadStopFile(sc, g_knit_data);
            if(ret)
            {
                printf("read stop file: %d\n", ret);
            }
            else
            {
                SendYarnStopParam(g_knit_data);
            }
            SetProgressData(g_knit_data, 90,  (char *)GetString(923));
            SendMessage(GetProgressHwnd(g_knit_data), MSG_UPDATE, 0, 0);
            sc[len] = 0;
            strcat(sc, CONF_FILE_SUFFIX);
            printf("read data from :%s\n",sc);
            ret = ReadConfigureFile(sc, g_knit_data);
            if(ret)
            {
                printf("failed to read conf file %s: %d\n", sc, ret);
            }
            SendExchangedYarn(g_knit_data);
            //backpiece
            DivideBackPiece(g_knit_data);
            InitBackPiece(g_knit_data);
            //need to reset
            SetWhichNeedDo(g_knit_data, NEED_DO_RESET_START);
            ret = WriteKnitFile(KNIT_PARAM_FILE, g_knit_data);
            if(ret)
            {
                printf("write knit param: %d\n", ret);
            }
            else
            {
                MH_FILE_SYNC();
            }
        }
    }
FAIL:
    return ret;
}
/*
 *
 */
static void OnPageUp(HWND hWnd)
{
    WND_EXTRA_DATA data;
    data.value = GetWindowAdditionalData(hWnd);
    if(data.pwnd_file)
    {
        if(data.pwnd_file->over_top < FILE_LIST_NUM_BY_FILE_BMP)
        {
            data.pwnd_file->over_top = 0;
        }
        else
        {
            data.pwnd_file->over_top -= FILE_LIST_NUM_BY_FILE_BMP;
        }
        ShowFileList(hWnd);
    }
}
/*
 *
 */
static void OnPageDown(HWND hWnd)
{
    WND_EXTRA_DATA data;
    data.value = GetWindowAdditionalData(hWnd);
    if(data.pwnd_file)
    {
        data.pwnd_file->over_top += FILE_LIST_NUM_BY_FILE_BMP;
        if(data.pwnd_file->over_top + FILE_LIST_NUM_BY_FILE_BMP > data.pwnd_file->list_size)
        {
            if(data.pwnd_file->list_size > FILE_LIST_NUM_BY_FILE_BMP)
            {
                data.pwnd_file->over_top = data.pwnd_file->list_size - FILE_LIST_NUM_BY_FILE_BMP;
            }
            else
            {
                data.pwnd_file->over_top = 0;
            }
        }
        ShowFileList(hWnd);
    }
}
/*
 *
 */
static void OnBtnCmd(HWND hWnd, int nID)
{
    switch(nID)
    {
    case ID_ESC:
    case ID_FALSE:
        {
            PostMessage (hWnd, MSG_CLOSE, 0, 0);
        }
        break;
    case ID_TRUE:
        {
            HWND hTmd = CreateProgressAllWindow(hWnd);
            SetProgressHwnd(g_knit_data, hTmd);
            SetProgressData(g_knit_data, 5, (char *)GetString(923));
            SendMessage(GetProgressHwnd(g_knit_data), MSG_UPDATE, 0, 0);
             if(STATE_STOP == GetRunState(g_knit_data))
            {
                int ret = SelectKnitFile(hWnd);
                if(!ret)
                {
                    if(g_hwnd_run > 0)
                    {
                        FlushRunWndFile(g_hwnd_run);
                    }
                }
                const char *sc = ret ? GetString(469) : GetString(470);

                SetProgressData(g_knit_data, 100, (char *)sc);
                SendMessage(GetProgressHwnd(g_knit_data), MSG_UPDATE, 0, 0);

                ThrowAwayMessages(hWnd);
            }
        }
        break;
    case (IDC_EDIT1)...(IDC_EDIT1 + FILE_LIST_NUM_BY_FILE_BMP - 1):
        {
            WND_EXTRA_DATA data;
            data.value = GetWindowAdditionalData(hWnd);
            if(data.pwnd_file_select)
            {
                data.pwnd_file_select->active = nID - IDC_EDIT1;
            }
        }
        break;
    default:
        break;
    }
}
/*
 *
 */
static void OnKeyDown(HWND hWnd, uint key)
{
    if(key == SCANCODE_ESCAPE)
    {
        OnBtnCmd(hWnd, ID_ESC);
    }
    else if(key == SCANCODE_F1)
    {
        WND_FILE *p = (WND_FILE*)GetWindowAdditionalData(hWnd);
        if(p)
        {
            p->active = 0;
            p->over_top = 0;
            if(p->file_list)
            {
                if(p->sort_file_type == SORT_FILE_BY_NAME_UP)
                {
                    p->sort_file_type = SORT_FILE_BY_NAME_DOWN;
                }
                else
                {
                    p->sort_file_type = SORT_FILE_BY_NAME_UP;
                }
                SortFileList(p->file_list, p->list_size, p->sort_file_type);
                ShowFileList(hWnd);
                SetRunFileSortType(p->sort_file_type);
            }
        }
    }
    else if(key == SCANCODE_F2)
    {
        WND_FILE *p = (WND_FILE*)GetWindowAdditionalData(hWnd);
        if(p)
        {
            p->active = 0;
            p->over_top = 0;
            if(p->file_list)
            {
                if(p->sort_file_type == SORT_FILE_BY_SIZE_UP)
                {
                    p->sort_file_type = SORT_FILE_BY_SIZE_DOWN;
                }
                else
                {
                    p->sort_file_type = SORT_FILE_BY_SIZE_UP;
                }
                SortFileList(p->file_list, p->list_size, p->sort_file_type);
                ShowFileList(hWnd);
                SetRunFileSortType(p->sort_file_type);
            }
        }
    }
    else if(key == SCANCODE_F3)
    {
        WND_FILE *p = (WND_FILE*)GetWindowAdditionalData(hWnd);
        if(p)
        {
            p->active = 0;
            p->over_top = 0;
            if(p->file_list)
            {
                if(p->sort_file_type == SORT_FILE_BY_TIME_DOWN)
                {
                    p->sort_file_type = SORT_FILE_BY_TIME_UP;
                }
                else
                {
                    p->sort_file_type = SORT_FILE_BY_TIME_DOWN;
                }
                SortFileList(p->file_list, p->list_size, p->sort_file_type);
                ShowFileList(hWnd);
                SetRunFileSortType(p->sort_file_type);
            }
        }
    }
    else if(key == SCANCODE_CURSORBLOCKUP)
    {
        WND_FILE *p = (WND_FILE*)GetWindowAdditionalData(hWnd);
        if(p)
        {
            if(p->active)
            {
                --p->active;
                SetFocusChild(GetDlgItem(hWnd, IDC_EDIT1 + p->active));
            }
            else if(p->over_top)
            {
                --p->over_top;
                ShowFileList(hWnd);
            }
        }
    }
    else if(key == SCANCODE_CURSORBLOCKDOWN)
    {
        WND_FILE *p = (WND_FILE*)GetWindowAdditionalData(hWnd);
        if(p)
        {
            if(p->active + p->over_top + 1 < p->list_size)
            {
                if(p->active + 1 < FILE_LIST_NUM_BY_FILE_BMP)
                {
                    ++p->active;
                    SetFocusChild(GetDlgItem(hWnd, IDC_EDIT1 + p->active));
                }
                else
                {
                    ++p->over_top;
                    ShowFileList(hWnd);
                }
            }
        }
    }
    else if(key == SCANCODE_ENTER)
    {
        OnBtnCmd(hWnd, ID_TRUE);
    }
    else if(key == SCANCODE_PAGEUP)
    {
        OnPageUp(hWnd);
    }
    else if(key == SCANCODE_PAGEDOWN)
    {
        OnPageDown(hWnd);
    }
}
/*
 *
 */
static int FileSelectWinProc(HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
    case MSG_CREATE:
        {
            CreateFileSelectInterface(hWnd);
        }
        break;
    case MSG_PAINT:
        {
            HDC hdc = GetClientDC(hWnd);
            RECT rect;
            GetClientRect(hWnd, &rect);
            rect.left += 100;
            rect.top += MASTER_TITLE_TOP;
            rect.right -= 100;
            rect.bottom = rect.top + MASTER_TITLE_HEIGHT - 1;
            SetBkMode(hdc, BM_TRANSPARENT);
            SetTextColor(hdc, COLOR_HQ_YELLOW(hdc));
            DrawText(hdc, GetWindowCaption (hWnd), -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
            ReleaseDC(hdc);
        }
        break;
    case MSG_CLOSE:
        {
            HWND hHosting = GetHosting(hWnd);
            if(hHosting != HWND_INVALID)
            {
                EnableWindow(hHosting, TRUE);
            }
            DestroyMainWindow(hWnd);
            MainWindowCleanup(hWnd);
        }
        return 0;
    case MSG_DESTROY:
        {
            ShowWindow(hWnd, SW_HIDE);
            DestroyAllControls(hWnd);
            WND_FILE *p = (WND_FILE*)GetWindowAdditionalData(hWnd);
            if(p)
            {
                free(p->file_list);
                free(p);
                SetWindowAdditionalData(hWnd, 0);
            }
        }
        break;
    case MSG_KEYDOWN:
        {
            OnKeyDown(hWnd, wParam);
        }
        return 0;
    case MSG_ERASEBKGND:
        {
            DrawBitmapBkGnd(hWnd, (RECT*)lParam, &g_bmp[BMP_FILE]);
        }
        return 0;
    case MSG_COMMAND:
        {
            OnBtnCmd(hWnd, LOWORD(wParam));
        }
        return 0;
    }
    return DefaultMainWinProc(hWnd, message, wParam, lParam);
}
/*
 *
 */
HWND CreateFileSelectWindow(HWND hHosting)
{
    int x = (CX_SCREEN - CX_FILE_BMP) / 2;
    int y = (CY_SCREEN - CY_FILE_BMP) / 2;

    MAINWINCREATE CreateInfo;
    CreateInfo.dwStyle = WS_VISIBLE;
    CreateInfo.dwExStyle = WS_EX_NONE;
    CreateInfo.spCaption = MW_FILE_SELECT_CAPTION;
    CreateInfo.hMenu = 0;
    CreateInfo.hCursor = GetSystemCursor(0);
    CreateInfo.hIcon = 0;
    CreateInfo.MainWindowProc = FileSelectWinProc;
    CreateInfo.lx = x;
    CreateInfo.ty = y;
    CreateInfo.rx = x + CX_FILE_BMP;
    CreateInfo.by = y + CY_FILE_BMP;
    CreateInfo.iBkColor = COLOR_lightwhite;
    CreateInfo.dwAddData = 0;
    CreateInfo.hHosting = hHosting;
    HWND hWnd = CreateMainWindow(&CreateInfo);
    if(hWnd != HWND_INVALID)
    {
        EnableWindow(hHosting, FALSE);
        SetFocusChild(GetDlgItem(hWnd, IDC_EDIT1));
        ShowWindow(hWnd, SW_SHOWNORMAL);
    }
    return hWnd;
}
